package ext.tianma.part.Interface.Until;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import ext.com.iba.IBAUtil;
import ext.tianma.bom.service.FinishedGoodService;
import ext.tianma.bom.util.LWCUtil;
import ext.tianma.bom.util.StringUtil;
import wt.csm.navigation.ClassificationNode;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.iba.value.DefaultAttributeContainer;
import wt.iba.value.IBAHolder;
import wt.iba.value.IBAValueUtility;
import wt.iba.value.ReferenceValue;
import wt.iba.value.litevalue.AbstractValueView;
import wt.iba.value.litevalue.URLValueDefaultView;
import wt.iba.value.service.IBAValueHelper;
import wt.part.WTPart;
import wt.pds.StatementSpec;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTContext;
import wt.util.WTException;

/**
 * 针对MDM项目获取分类属性
 * 
 * @author laichao_sun
 *
 */
public class ClassificationMDM {

	/***
	 * 根据节点名称获取分类属性
	 * 
	 * @param classifictionName
	 * @throws WTException
	 * @throws RemoteException
	 * @throws JSONException
	 */
	@SuppressWarnings("deprecation")
	public static List<HashMap<String, String>> getClassifictionIba(String classifictionName, WTPart part) {
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		try {
			ClassificationNode node = null;
			QuerySpec queryspec = new QuerySpec(wt.iba.value.ReferenceValue.class);
			queryspec.appendSearchCondition(new SearchCondition(wt.iba.value.ReferenceValue.class,
					"theIBAHolderReference.key", "=", part.getPersistInfo().getObjectIdentifier()));

			QueryResult queryresult = PersistenceHelper.manager.find(queryspec);
			while (queryresult.hasMoreElements()) {
				wt.iba.value.ReferenceValue value = (ReferenceValue) queryresult.nextElement();
				Persistable obj = value.getIBAReferenceableReference().getObject();
				if (obj instanceof ClassificationNode) {
					node = (ClassificationNode) obj;
				}
			}

			if (node != null) {
				Hashtable values = IBAUtil.getIBAValues(node);
				Iterator ibaiter = values.keySet().iterator();
				while (ibaiter.hasNext()) {
					String key = ibaiter.next().toString();
					if (values.get(key) != null) {
						String strMesg;
						String displayname = FinishedGoodService.queryDisplayName(key, "string");
						if (StringUtil.isNullOrEmpty(displayname)) {
							// 若stringdefination表中未找到 则查找float
							displayname = FinishedGoodService.queryDisplayName(key, "float");
							if ("".equals(displayname)) {
								displayname = FinishedGoodService.queryDisplayName(key, "timestamp");
								if ("".equals(displayname)) {
									displayname = FinishedGoodService.queryDisplayName(key, "integer");
									
										strMesg = (String) IBAUtil.getIBAValue(part, key);
										HashMap<String, String> iabList = new HashMap<String, String>();
										iabList.put("displayname", displayname);
										iabList.put("key", key);
										iabList.put("value", strMesg == null ? "" : strMesg);
										list.add(iabList);
										continue;
									
								}else {
									strMesg = (String) IBAUtil.getIBAValue(part, key);
									HashMap<String, String> iabList = new HashMap<String, String>();
									iabList.put("displayname", displayname);
									iabList.put("key", key);
									iabList.put("value", strMesg == null ? "" : strMesg);
									list.add(iabList);
									continue;
								}
							}else {
								//strMesg = getIBAValueMap(part, key);// 获取属性值
								strMesg = (String) IBAUtil.getIBAValue(part, key);
								HashMap<String, String> iabList = new HashMap<String, String>();
								iabList.put("displayname", displayname);
								iabList.put("key", key);
								iabList.put("value", strMesg == null ? "" : strMesg);
								list.add(iabList);
								continue;
							}
							
						}else {
							strMesg = (String) IBAUtil.getIBAValue(part, key);
							if(StringUtil.isNullOrEmpty(strMesg)) {
								HashMap<String, String> iabList = new HashMap<String, String>();
								iabList.put("displayname", displayname);
								iabList.put("key", key);
								iabList.put("value", strMesg == null ? "" : strMesg);
								list.add(iabList);
							}else {
								strMesg = getIBAValueMap(part, key);// 获取属性值
								HashMap<String, String> iabList = new HashMap<String, String>();
								iabList.put("displayname", displayname);
								iabList.put("key", key);
								iabList.put("value", strMesg == null ? "" : strMesg);
								list.add(iabList);
							}
						}



					}
				}
				// System.out.println("SLC ==获取到的分类属性结合===> "+list.toString());
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;

	}

	/**
	 * 获取分类属性的key,value
	 * 
	 * @param ibaHolder
	 * @param key
	 * @return value
	 * @throws Exception
	 */
	public static String getIBAValueMap(IBAHolder ibaHolder, String key) throws Exception {
		HashMap ibaValueMap = new HashMap();
		ibaHolder = IBAValueHelper.service.refreshAttributeContainer(ibaHolder, null,
				WTContext.getContext().getLocale(), null);

		DefaultAttributeContainer defaultattributecontainer = (DefaultAttributeContainer) ibaHolder
				.getAttributeContainer();
		if (defaultattributecontainer != null) {
			AbstractValueView aabstractvalueview[] = defaultattributecontainer.getAttributeValues();
			for (int i = 0; i < aabstractvalueview.length; i++) {
				String name = aabstractvalueview[i].getDefinition().getName();
				String value = null;
				if (aabstractvalueview[i] instanceof URLValueDefaultView) {
					URLValueDefaultView urlv = (URLValueDefaultView) aabstractvalueview[i];
					value = "" + urlv.getDescription() + "";
				} else {
					value = IBAValueUtility.getLocalizedIBAValueDisplayString(aabstractvalueview[i],
							WTContext.getContext().getLocale());
				}

				Object eValue = (Object) ibaValueMap.get(name);
				if (eValue == null) {
					ibaValueMap.put(name, value);
				} else {
					ArrayList al = new ArrayList();
					if (eValue instanceof ArrayList) {
						al = (ArrayList) eValue;
						al.add(value);
					} else {
						al.add(eValue);
						al.add(value);
					}
					ibaValueMap.put(name, al);
				}
			}
		}
		String value;
		if (ibaValueMap.get(key).toString().contains("[") || ibaValueMap.get(key).toString().contains("]")) {
			value = StringUtils.strip(ibaValueMap.get(key).toString(), "[]");
			value = replaceBlank(value);
		} else {
			value = ibaValueMap.get(key).toString();
		}
		return value;
	}

	// 去除所有空白格
	public static String replaceBlank(String str) {
		String dest = null;
		if (str == null) {
			return dest;
		} else {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n|&nbsp;");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
			return dest;
		}
	}

}
